package design.simdi.code.final_paper.site.panels.blog;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import design.simdi.code.final_paper.core.entities.Mesaj;
import design.simdi.code.final_paper.core.enums.MessageStatus;
import design.simdi.code.final_paper.site.pages.BasePage;
import design.simdi.code.final_paper.site.panels.OneButtonPanel;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 9:03:28 PM)
 * 
 */
public class ViewMesajPanel extends OneButtonPanel {

	private static final long serialVersionUID = 1L;
	private Mesaj mesaj;
	private Component backComponent;

	private TextField<String> dest;
	private TextField<String> exped;
	private TextField<String> subject;
	private MultiLineLabel content;

	public ViewMesajPanel(String id, IModel<Mesaj> model, Component backComponent) {
		super(id, model);
		this.backComponent = backComponent;
	}

	@Override
	public void panelInit() {
		super.panelInit();
		setOutputMarkupId(true);

		mesaj = (Mesaj) getDefaultModelObject();

		getForm().add(dest = new TextField<String>("dest", new Model<String>(mesaj.getDestinatar().toString())));
		getForm().add(exped = new TextField<String>("exped", new Model<String>(mesaj.getExpeditor().toString())));
		getForm().add(subject = new TextField<String>("subject", new PropertyModel<String>(mesaj, "title")));
		getForm().add(content = new MultiLineLabel("content", new PropertyModel<String>(mesaj, "content")));

		dest.setEnabled(false);
		exped.setEnabled(false);
		subject.setEnabled(false);
		content.setEnabled(false);

		if (mesaj.getStatus() == MessageStatus.NEW && getCurrentPerson().getId() == mesaj.getDestinatar().getId())
			getApplicationService().markMesajRead(mesaj);
	}

	@Override
	public void onCANCELButtonSubmit(AjaxRequestTarget target) {

		Page page = this.getPage();

		page.get("content:tabs:panel").replaceWith(backComponent);

		target.addComponent(page.get("content"));

		target.addComponent(page.get(BasePage.ERRORS_PANEL));
	}

}
